find_package(Protobuf REQUIRED)
find_program(PROTOC protoc)

message("Generate proto files by package ${CMAKE_CURRENT_SOURCE_DIR}")
set(proj_dir ${CMAKE_CURRENT_SOURCE_DIR})

macro(generate_cpp_pb_files)
    unset(proto_h_files)
    unset(proto_cc_files)
    set(pb_file_path ${proj_dir}/proto)
    file(GLOB proto_files ${pb_file_path}/*.proto)

    set(proto_import_path "-I${CMAKE_CURRENT_SOURCE_DIR}/proto/")
    #foreach(proto_pkg ${proto_import_package})
    #    get_filename_component(ABSOLUTE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../${proto_pkg}/proto/ ABSOLUTE)
    #    set(proto_import_path "${proto_import_path} -I ${ABSOLUTE_PATH}")
    #    include_directories(
    #        ${ABSOLUTE_PATH}
    #    )
    #endforeach(proto_pkg ${proto_import_package})

    foreach(pb_file ${proto_files})
        get_filename_component(pb_file_name ${pb_file} NAME_WE)
        set(proto_h_files ${proto_h_files} ${proj_dir}/proto/${pb_file_name}.pb.h)
        set(proto_cc_files ${proto_cc_files} ${proj_dir}/proto/generated_pb_files/${pb_file_name}.pb.cc)
        
        execute_process(COMMAND protoc ${pb_file} --cpp_out=${CMAKE_CURRENT_SOURCE_DIR}/proto/generated_pb_files/ ${proto_import_path})
        execute_process(COMMAND cp -f ${CMAKE_CURRENT_SOURCE_DIR}/proto/generated_pb_files/${pb_file_name}.pb.h ${CMAKE_CURRENT_SOURCE_DIR}/include/${PROJECT_NAME}/${pb_file_name}.pb.h)
        #execute_process(COMMAND ln -f -s ${CMAKE_CURRENT_SOURCE_DIR}/proto/generated_pb_files/${pb_file_name}.pb.h ${CMAKE_CURRENT_SOURCE_DIR}/include/${PROJECT_NAME}/${pb_file_name}.pb.h)
    endforeach(pb_file ${proto_files})
endmacro()